var util = require('../../utils/util.js');
Component({
  properties: {
    //控制显示
    show: {
      type: Boolean,
      value: false
    },
    tjxlPk: {
      type: String
    },
    tjxlTitle: {
      type: String
    },
    url: {
      type: String
    },
    pkList: {
      type: Array,
      value: []
    }
  },
  data: {
    startDate: '', // 限制选择的起始日期
    arriveDate: '', // 到达日期
    peopleNum: '', // 出行人数
    lvList: ['二级巡视员', '一级巡视员'],
    lvIndex: '',// 人员最高级别
    unitName: '', // 单位名称
    jbrxm: '', // 经办人姓名
    jbrlxdh: '', // 经办人联系电话
    jbryx: '', // 经办人邮箱
  },
  pageLifetimes: {
    show() {
      

      // this.init();
    },
  },
  methods: {
    //初始化
    init(e) {// 获取当前日期
      const today = new Date();
      const year = today.getFullYear();
      const month = today.getMonth() + 1; // 月份从 0 开始，需要加 1
      const day = today.getDate();

      // 格式化日期为 YYYY-MM-DD
      const formattedDate = `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;

      // 设置 startDate
      this.setData({
        startDate: formattedDate,
        arriveDate: formattedDate
      });
      this.getBmxx();
    },
    //到达日期选择
    changeDate(e) {
      this.setData({
        arriveDate: e.detail.value
      })
    },
    //人员最高级别选择
    changeLv(e) {
      console.log(e)
      this.setData({
        lvIndex: e.detail.value
      })
    },
    //关闭弹窗
    closePopup() {
      this.setData({ show: false })
    },
    //生成行程单按钮
    createTripBtn(e) {
      const { arriveDate, peopleNum, lvIndex, unitName, jbrxm, jbrlxdh, jbryx } = this.data;
      //校验必填项
      if (!arriveDate) return util.showMsg({ msg: "请选择到达日期" });
      if (!peopleNum) return util.showMsg({ msg: "请输入人数" });
      if (!lvIndex) return util.showMsg({ msg: "请选择人员最高级别" });
      if (!unitName) return util.showMsg({ msg: "请输入单位名称" });
      if (!jbrxm) return util.showMsg({ msg: "请输入经办人姓名" });
      if (!jbrlxdh) return util.showMsg({ msg: "请输入经办人联系电话" });
      if (!jbryx) return util.showMsg({ msg: "请输入经办人邮箱" });

      if (Number(peopleNum) < 1) return util.showMsg({ msg: "人数不能少于1人" });
      if (!util.validatePhoneNumber(jbrlxdh)) return util.showMsg({ msg: "联系电话格式错误" });
      if (!util.validateEmail(jbryx)) return util.showMsg({ msg: "邮箱格式错误" });

      this.generateTravelOrder();
    },
    //生成行程单接口
    generateTravelOrder() {
      const _this = this;
      const { tjxlPk, arriveDate, peopleNum, lvIndex, unitName, jbrxm, jbrlxdh, jbryx } = this.data;
      let url = '/itf/common/hongpei/travelorder/generateTravelOrder.json';
      let params = {
        "tjxl_pk": tjxlPk,
        "ddrq_str": arriveDate,
        "rs": peopleNum,
        "ryzgjb": _this.data.lvList[lvIndex],
        "dwmc": unitName,
        "jbrxm": jbrxm,
        "jbr_lxdh": jbrlxdh,
        "jbr_yx": jbryx
      };
      if (this.data.url) {
        url = this.data.url;
        params.zyd_pk_list = this.data.pkList;
      }
      wx.showLoading({
        title: '生成行程单中...',
        mask: true
      })
      util.request(url, params, "POST").then(res => {
        wx.hideLoading();
        if (res.success) {
          _this.setData({ show: false })
          util.showMsg({
            msg: "生成行程单成功",
            icon: 'success',
            duration: 1000,
            success: () => {
              wx.navigateTo({
                url: '/pagesTripbill/tripPreview/tripPreview?billid=' + res.data,
              })
            }
          });
        } else {
          util.showMsg({ msg: res.message || res.msg });
        }
      });
    },
    //获取最近报名信息
    getBmxx() {
      const _this = this;
      util.request('/itf/common/bmxx/getBmxx.json').then(res => {
        if (res.success) {
          let result = res.data;
          if (result) {
            let lvIndex = '';
            _this.data.lvList.forEach((item, index) => {
              if (item == result.ryzgjb) {
                lvIndex = index + ''
              }
            })
            _this.setData({
              // arriveDate: result.ddrq,
              peopleNum: result.rs,
              lvIndex,
              unitName: result.dwmc,
              jbrxm: result.jbrxm,
              jbrlxdh: result.jbr_lxdh,
              jbryx: result.jbr_yx,
            })
          }
        } else {
          util.showMsg({ msg: res.message || res.msg });
        }
      });
    }
  }
})